﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrazyWorship1.PropertyHandler;
using System.Globalization;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
using System.Windows;

namespace CrazyWorship1.Controllers
{
   public class TextShakeController:ControllerBase
    {
       private double _XShake, _YShake, _ZShake, _Duration;
       [DataAttribute("XShake", typeof(DoublePropertyHandler),"TextShakeControllerDefaultXShake")]
       public string XShake
       {
           get
           {
               return _XShake.ToString(CultureInfo.InvariantCulture);
           }
           set
           {
               _XShake = Convert.ToDouble(value, CultureInfo.InvariantCulture);
           }
       }
       [DataAttribute("YShake", typeof(DoublePropertyHandler), "TextShakeControllerDefaultYShake")]
       public string YShake
       {
           get
           {
               return _YShake.ToString(CultureInfo.InvariantCulture);
           }
           set
           {
               _YShake = Convert.ToDouble(value, CultureInfo.InvariantCulture);
           }
       }
       [DataAttribute("ZShake", typeof(DoublePropertyHandler), "TextShakeControllerDefaultZShake")]
       public string ZShake
       {
           get
           {
               return _ZShake.ToString(CultureInfo.InvariantCulture);
           }
           set
           {
               _ZShake = Convert.ToDouble(value, CultureInfo.InvariantCulture);
           }
       }
       [DataAttribute("Duration", typeof(DoublePropertyHandler), "TextShakeControllerDefaultDuration")]
       public string Duration
       {
           get
           {
               return _Duration.ToString(CultureInfo.InvariantCulture);
           }
           set
           {
               _Duration= Convert.ToDouble(value, CultureInfo.InvariantCulture);
           }
       }
       DoubleAnimation px,py,pz,p2x,p2y,p2z;
       PerspectiveCamera c, c2;
       Storyboard a;
       public override void Go()
       {
           Duration d = new Duration(new TimeSpan(0, 0, 0, 0, (int)_Duration));
           c = CurrentState.Viewers[0].cam;
           c2 = CurrentState.Viewers[1].cam;
           Transform3DGroup tc1 = new Transform3DGroup();
           Transform3DGroup tc2 = new Transform3DGroup();
           c.Transform = (Transform3D)tc1;
           c2.Transform = (Transform3D)tc2;
           TranslateTransform3D tl1 = new TranslateTransform3D();
           TranslateTransform3D tl2 = new TranslateTransform3D();
           tc1.Children.Add(tl1);
           tc2.Children.Add(tl2);
           Point3D p = new Point3D(_XShake, _YShake, _ZShake);

           px = new DoubleAnimation();
           px.To = p.X;
           px.Duration =d ;
           py = new DoubleAnimation();
           py.To = p.Y;
           py.Duration = d;
           pz = new DoubleAnimation();
           pz.To = p.Z;
           pz.Duration = d;

           p2x = new DoubleAnimation();
           p2x.To = p.X;
           p2x.Duration = d;
           p2y = new DoubleAnimation();
           p2y.To = p.Y;
           p2y.Duration = d;
           p2z = new DoubleAnimation();
           p2z.To = p.Z;
           p2z.Duration =d;


           
         //  CurrentState.Viewers[0].UnregisterName("MainCamera");
          // CurrentState.Viewers[0].UnregisterName("MainCamera2");

           CurrentState.Viewers[0].RegisterName("MainCamera", tl1);
           CurrentState.Viewers[0].RegisterName("MainCamera2", tl2);

           a = new Storyboard();
           Storyboard.SetTargetName(px, "MainCamera");
           Storyboard.SetTargetProperty(px, new PropertyPath(TranslateTransform3D.OffsetXProperty));
           Storyboard.SetTargetName(py, "MainCamera");
           Storyboard.SetTargetProperty(py, new PropertyPath(TranslateTransform3D.OffsetYProperty));
           Storyboard.SetTargetName(pz, "MainCamera");
           Storyboard.SetTargetProperty(pz, new PropertyPath(TranslateTransform3D.OffsetZProperty));

           Storyboard.SetTargetName(p2x, "MainCamera2");
           Storyboard.SetTargetProperty(p2x, new PropertyPath(TranslateTransform3D.OffsetXProperty));
           Storyboard.SetTargetName(p2y, "MainCamera2");
           Storyboard.SetTargetProperty(p2y, new PropertyPath(TranslateTransform3D.OffsetYProperty));
           Storyboard.SetTargetName(p2z, "MainCamera2");
           Storyboard.SetTargetProperty(p2z, new PropertyPath(TranslateTransform3D.OffsetZProperty));


           a.Children.Add(px);
           a.Children.Add(py);
           a.Children.Add(pz);
           a.Children.Add(p2x);
           a.Children.Add(p2y);
           a.Children.Add(p2z);
           rnd = new Random();
           a.Completed += new EventHandler(a_Completed);
           a.Begin(CurrentState.Viewers[0]);
          
       }
       Random rnd;
       double newX, newY, newZ;
       void a_Completed(object sender, EventArgs e)
       {
          newX=(1-(2*rnd.NextDouble()))*_XShake;
          newY=(1-(2*rnd.NextDouble()))*_YShake;
          newZ = (1 - (2 * rnd.NextDouble())) * _ZShake;

          px.To = newX;
          py.To = newY;
          pz.To = newZ;
           
          p2x.To = newX;
          p2y.To = newY;
          p2z.To = newZ;

           a.Begin(CurrentState.Viewers[0]);
       }
    }
}
